package com.niuxiaofa.newlaw.Tools;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2016/1/22.
 */
public class ToActivityUtil extends Activity {

    /**
     * @param packageContext
     * @param action         含操作的Intent
     * @Description: 隐式启动, 跳转
     */
    public static void startActivityIntentSafe(Context packageContext,
                                               Intent action) {
        // Verify it resolves
        PackageManager packageManager = packageContext.getPackageManager();
        List activities = packageManager.queryIntentActivities(action,
                PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafe = activities.size() > 0;

        // Start an activity if it's safe
        if (isIntentSafe) {
            packageContext.startActivity(action);
        }
    }


//    public static toNextAnimator(Context packageContext, Class<?> cls){
//
//        ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(getActivity(), user_icon, "user_icon");
//        Intent intent2 = new Intent(getActivity(), LoginActivity.class);
//        startActivity(intent2, activityOptions.toBundle());
//
//    }


    /**
     * @param cls to,一般传XXXActivity.class
     * @Description: 跳转
     */
    public static void toNextActivity(Class<?> cls) {
        Intent i = new Intent(UIUtils.getActivity(), cls);
        UIUtils.getActivity().startActivity(i);
    }

    /**
     * @param cls
     * @param keyvalues 需要传进去的String参数{{key1,values},{key2,value2}...}
     * @Description: 跳转, 带参数的方法;需要其它的数据类型,再继续重载吧
     */
    public static void toNextActivity(Class<?> cls,
                                      String[][] keyvalues) {
        Intent i = new Intent(UIUtils.getActivity(), cls);
        for (String[] strings : keyvalues) {
            i.putExtra(strings[0], strings[1]);
        }
        UIUtils.getActivity().startActivity(i);
    }


    //    communityBean = (CommunityBean.DataEntity.ListEntity) getIntent().getSerializableExtra("CommunityBean");
    public static void toNextActivityClass(Class<?> cls, String text,  Serializable bean) {

        Intent intent = new Intent();
        intent.setClass(UIUtils.getActivity(), cls);
        Bundle bundle = new Bundle();
        bundle.putSerializable(text, bean);
        intent.putExtras(bundle);
        UIUtils.getActivity().startActivity(intent);

    }


//    public static void toNextActivity(Context packageContext, Class<?> cls,)


    public static void toNextActivityAndFinish(Context packageContext,
                                               Class<?> cls) {
        Intent i = new Intent(packageContext, cls);
        packageContext.startActivity(i);

        ((Activity) packageContext).finish();
    }

    public static void toWebsite(Context packageContext, String url) {
        Uri uri2 = Uri.parse(url);
        Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
        packageContext.startActivity(intent2);
    }

    public static void finish(Activity activity) {
        activity.finish();
    }


}
